﻿

using System;
using System.Runtime.Serialization;
using System.Collections.Generic;
using COM.Rights.Model;


using System.Text;
using System.Web;

namespace COM.Rights.BLL
{   
    public partial class TFunction 
    {
        List<string> RoleNameList = new List<string>();
        
        partial void OnBLLCreated() { ConfigName = "Rights"; }
        
        public List<string> GetRoleNameList()
        {
            return RoleNameList;
        }
        public Dictionary<string, TFunctionInfo> GetFunctionByUser(string gid)
        {
            List<string> RoleGidList = new List<string>();
            List<TFunctionInfo> TFunctionInfoList = new List<TFunctionInfo>();
            Dictionary<string, TFunctionInfo> result = new Dictionary<string, TFunctionInfo>();

            //1.根据用户GID取出用户所归属的所有组织机构GID
            List<string> TouGidList = new List<string>();
            TOUTTTUser _TOUTTTUser = new TOUTTTUser();
            List<TOUTTTUserInfo> _TOUTTTUserInfoList = new List<TOUTTTUserInfo>();
            List<DBParameter> paras = new List<DBParameter>();
            paras.Add(new DBParameter("TUser_GID", gid));
            _TOUTTTUserInfoList = _TOUTTTUser.GetList("TOU_GID", " TUser_GID=@TUser_GID", "", paras);
            if (_TOUTTTUserInfoList != null)
            {
                for (int i = 0; i < _TOUTTTUserInfoList.Count; i++)
                {
                    if (!TouGidList.Contains(_TOUTTTUserInfoList[i].TOU_GID))
                    {
                        TouGidList.Add(_TOUTTTUserInfoList[i].TOU_GID);
                    }
                }
                
                //2.根据用户组织的GID取出用户所归属的所有角色GID
                TRoleTTTOU _TRoleTTTOU = new TRoleTTTOU();
                for (int i = 0; i < TouGidList.Count; i++)
                {
                    List<DBParameter> paras2 = new List<DBParameter>();
                    paras2.Add(new DBParameter("TOU_GID", TouGidList[i]));
                    List<TRoleTTTOUInfo> _TRoleTTTOUInfoList = new List<TRoleTTTOUInfo>();
                    _TRoleTTTOUInfoList = _TRoleTTTOU.GetList("TRole_GID", "TOU_GID=@TOU_GID", "", paras2);
                    if (_TRoleTTTOUInfoList != null)
                    {
                        for(int j=0;j<_TRoleTTTOUInfoList.Count;j++)
                        {
                            if (!RoleGidList.Contains(_TRoleTTTOUInfoList[j].TRole_GID))
                            {
                                RoleGidList.Add(_TRoleTTTOUInfoList[j].TRole_GID);
                            }
                        }
                    }
                }
            }
            
            //3.根据用户GID取出用户所归属的所有角色GID
            TRoleTTTUser _TRoleTTTUser = new TRoleTTTUser();
            List<TRoleTTTUserInfo> _TRoleTTTUserInfoList = new List<TRoleTTTUserInfo>();
            _TRoleTTTUserInfoList = _TRoleTTTUser.GetList("TRole_GID", "TUser_GID=@TUser_GID", "", paras);
            if (_TRoleTTTUserInfoList != null)
            {
                for (int i = 0; i < _TRoleTTTUserInfoList.Count; i++)
                {
                    if (!RoleGidList.Contains(_TRoleTTTUserInfoList[i].TRole_GID))
                    {
                        RoleGidList.Add(_TRoleTTTUserInfoList[i].TRole_GID);
                    }
                }
            }

            //4.根据角色GID取出所有的功能模块GID
            List<string> TFunctionGidList = new List<string>();
            TRoleTTTFunction _TRoleTTTFunction = new TRoleTTTFunction();

           
            TRole _TRole = new TRole();

            for (int i = 0; i < RoleGidList.Count; i++)
            {
                List<DBParameter> paras3 = new List<DBParameter>();
                paras3.Add(new DBParameter("TRole_GID", RoleGidList[i]));
                List<TRoleTTTFunctionInfo> _TRoleTTTFunctionInfo = new List<TRoleTTTFunctionInfo>();
                _TRoleTTTFunctionInfo = _TRoleTTTFunction.GetList("TFunction_GID", "TRole_GID=@TRole_GID", "", paras3);
                if (_TRoleTTTFunctionInfo != null)
                {
                    for (int j = 0; j < _TRoleTTTFunctionInfo.Count; j++)
                    {
                        if (!TFunctionGidList.Contains(_TRoleTTTFunctionInfo[j].TFunction_GID))
                        {
                            TFunctionGidList.Add(_TRoleTTTFunctionInfo[j].TFunction_GID);
                        }
                    }
                }
                //取出用户对应的角色名称
                TRoleInfo _TRoleInfo = new TRoleInfo();
                _TRoleInfo = _TRole.GetModel(RoleGidList[i]);
                if (!RoleNameList.Contains(_TRoleInfo.Name))
                {
                    RoleNameList.Add(_TRoleInfo.Name);
                }
            }


            //5.根据功能模块GID取出所有的功能
            TFunction _TFunction = new TFunction();
            for (int i = 0; i < TFunctionGidList.Count; i++)
            {
                List<DBParameter> paras4 = new List<DBParameter>();
                paras4.Add(new DBParameter("TFunction_GID", TFunctionGidList[i]));
                TFunctionInfo _TFunctionInfo = new TFunctionInfo();
                _TFunctionInfo = _TFunction.GetModel("", "GID=@TFunction_GID", "", paras4);
                if (_TFunctionInfo != null)
                {
                    if (!TFunctionInfoList.Contains(_TFunctionInfo))
                    {
                        TFunctionInfoList.Add(_TFunctionInfo);
                    }
                }
            }

            //6.根据所有的功能生成一个功能字典并返回结果集
            for (int i = 0; i < TFunctionInfoList.Count; i++)
            {
                if (!result.ContainsKey(TFunctionInfoList[i].Code.ToUpper()))
                {
                    result.Add(TFunctionInfoList[i].Code.ToUpper(), TFunctionInfoList[i]);
                }
            }

            return result;
        }
       
        public List<TFunctionInfo> GetChildren(string id)
        {
            string filter = "PGID=@PGID";
            DBParameter dp = new DBParameter("PGID", id);

            List<DBParameter> dps = new List<DBParameter>();
            dps.Add(dp);
            List<TFunctionInfo> infos = GetList("", filter, "Sequence ASC", dps);

            List<TFunctionInfo> result = new List<TFunctionInfo>();
            result.AddRange(infos);
            foreach (TFunctionInfo info in infos)
            {
                result.AddRange(GetChildren(info.GID));
            }
            return result;
        }

        public List<TFunctionInfo> GetChild(string id)
        {
            string filter = "PGID=@PGID";
            DBParameter dp = new DBParameter("PGID", id);

            List<DBParameter> dps = new List<DBParameter>();
            dps.Add(dp);
            List<TFunctionInfo> infos = GetList("", filter, "Sequence ASC", dps);

            return infos;
        }

        public string CreateHTML(Dictionary<string, TFunctionInfo> functions)
        {
            List<TFunctionInfo> roots = GetChild("0");
            StringBuilder sb = new StringBuilder();
            List<TFunctionInfo> sons = null;

            string _tempHtml;

            foreach (TFunctionInfo info in roots)
            {

                if (!functions.ContainsKey(info.Code.ToUpper()))
                    continue;
                sb.Append("{title:'" + info.Name + "',autoScroll:true,border:false,iconCls:'nav',");
                sons = GetChild(info.GID);
                if (sons.Count > 0)
                {
                    _tempHtml = "<ul class=\"LeftNav\">";
                    foreach (TFunctionInfo son in sons)
                    {
                        if (!functions.ContainsKey(son.Code.ToUpper()))
                            continue;
                        _tempHtml += "<li style=\"margin-top:5px;\"><a target=\"main\" href=\"" + son.URI + "\"> " + son.Name + "</a></li>";
                    }
                    if (_tempHtml != "<ul>")
                    {
                        _tempHtml += "</ul>";
                        sb.Append("html:'" + _tempHtml + "'}");
                    }
                    else
                    {
                        sb.Append("html:''}");
                    }
                }
                else
                {
                    sb.Append("html:''}");
                }

                sb.Append(",");
            }

            return sb.ToString().TrimEnd(',');
        }
       
	}
}